/*
 * Copyright (C) 2011, Pavel Samko <bulldozerbsg@gmail.com>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the "Pavel Samko" nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

#ifndef VCTCPSERVER_H
#define VCTCPSERVER_H

#include "VCStd.h"

#define VCTCPSERVER_BUFF_SIZE 1024

class VCTCPServerClient;
class VCTCPServerClientThread: public VCThread
{
	friend class VCTCPServerClient;
public:
	VCTCPServerClientThread(VCTCPServerClient & mod);
	virtual ~VCTCPServerClientThread() {}

protected:
	virtual int Execute(void * data);
	virtual void OnExit(int exitcode);

private:
	VCTCPServerClient & m_mod;
	char m_recv_buff[VCTCPSERVER_BUFF_SIZE];
};

class VCTCPServerThread;
class VCTCPServer;
class VCTCPServerClient
{
	friend class VCTCPServerClientThread;
public:
	VCTCPServerClient(VC_SOCKET socket, VC_SOCKET_ADDR_IN * addr, VCTCPServerThread & server_thread);
	virtual ~VCTCPServerClient();

	void Disconnect();
	bool IsConnected();
	
	void Send(void * data, size_t data_size);

	VCTCPServer & GetServer();

protected:
	virtual void OnRecv(void * data, size_t data_size);  // need override
	virtual void OnDisconect(); // need override

private:
	bool m_needexit;
	VCTCPServerClientThread * m_thread;

	VC_SOCKET m_socket;
	VCMutex m_socket_mutex;

	VCBuffFIFO m_send_buff;
	VCMutex m_send_buff_mutex;

	VCTCPServerThread & m_server_thread;
};

class VCTCPServer;
class VCTCPServerThread: public VCThread
{
	friend class VCTCPServer;
	friend class VCTCPServerClientThread;
	friend class VCTCPServerClient;
public:
	VCTCPServerThread(VCTCPServer & mod);
	virtual ~VCTCPServerThread() {}
protected:
	virtual int Execute(void * data);
	virtual void OnExit(int exitcode);
private:
	VCTCPServer & m_mod;

	std::list<VCTCPServerClient *> m_clients;
	VCMutex m_clients_mutex;

	std::list<VCTCPServerClient *> m_disconnectedclients;
	VCMutex m_disconnectedclients_mutex;
};

class VCTCPServer
{
	friend class VCTCPServerThread;
public:
	VCTCPServer();
	virtual ~VCTCPServer();

	bool Listen(const char * on_ip, unsigned short on_port);
	void StopListening();
	bool IsListening();
	
protected:
	virtual VCTCPServerClient * OnConnect(VC_SOCKET socket, VC_SOCKET_ADDR_IN * addr, VCTCPServerThread & server_thread);  // need override

private:
	bool m_needstop;
	VCTCPServerThread * m_thread;
	
	VC_SOCKET m_socket;
	VCMutex m_socket_mutex;
};

#endif /* VCTCPSERVER_H */

